<?php

namespace Common\Lib\Payments;

/**
 * @class PaymentPlugin
 * @brief 支付插件抽象类
 */
abstract class PaymentPlugin {

    protected $paymentId = 0;
    protected $method = "post"; //表单提交模式
    protected $name = '';  //支付插件名称
    protected $version = 1.0;   //版本
    protected $callbackUrl = '';    //支付完成后，同步回调地址
    protected $serverCallbackUrl = '';    //异步通知地址
    protected $merchantCallbackUrl = '';  //支付中断返回
    protected $config;
    protected $reply;

    /**
     * @brief 构造函数
     * @param $payment_id 支付方式ID
     */
    public function __construct($payment,$backParam = array()) {
		$backParam = $backParam?$backParam:array();
        $this->paymentId = $payment['id'];
        $this->name = $payment['name'];
        //回调函数地址
        $this->callbackUrl = 'http://' . I('SERVER.HTTP_HOST') . U('Payment/callback',array_merge($backParam,array('_id' => $this->paymentId)));
        //回调业务处理地址
        $this->serverCallbackUrl = 'http://' . I('SERVER.HTTP_HOST') . U('Payment/callback', array_merge($backParam,array('_id' => $this->paymentId, '_notify' => 1)));
        //中断支付返回
        $this->merchantCallbackUrl = 'http://' . I('SERVER.HTTP_HOST') . U('Payment/callback_interrupt', array('_id' => $this->paymentId));

        if ($payment['config_param']) {
            $this->config = json_decode($payment['config_param'], true);
        }
    }

    public function config($key) {
        return isset($this->config[$key]) ? $this->config[$key] : '';
    }

    public function isNotify() {
        return false;
    }

    /**
     * 
     * @return Reply
     */
    public function reply() {
        if (!($this->reply instanceof Reply)) {
            $this->reply = new Reply();
        }
        return $this->reply;
    }

    /**
     * @brief 开始支付
     */
    public function doPay($sendData) {
        header("Content-type:text/html;charset=utf-8");
        $data = "";
        foreach ($sendData as $key => $item) {
            $data .= "<input type='hidden' name='{$key}' value='{$item}' />\r\n";
        }
        echo <<< EOF
<!DOCTYPE html>
<html lang="zh-CN">
    <head>
    <meta charset="utf-8">
    </head>
    <body>
        <p>please wait...</p>
        <form action="{$this->getSubmitUrl()}" method="{$this->method}">
{$data}
        </form>
    </body>
    <script type='text/javascript'>
        window.document.forms[0].submit();
    </script>
</html>
EOF;
    }

    /**
     * @brief 返回配置参数
     */
    public function configParam() {
        return array(
            'M_PartnerId' => '商户ID号',
            'M_PartnerKey' => '商户KEY密钥',
        );
    }

    /**
     * 是否同步返回
     * @return boolean
     */
    public function isReturn() {
        return true;
    }

    /**
     * 异步通知停止
     */
    abstract public function notifyStop();

    /**
     * 获取提交地址
     * @return string Url提交地址
     */
    abstract public function getSubmitUrl();

    /**
     * 获取要发送的数据数组结构
     * @param $payment array 要传递的支付信息
     * @return array
     */
    abstract public function getSendData($paymentInfo);

    /**
     * 异步支付回调
     * @param $ExternalData array  支付接口回传的数据
     * @return \Common\Lib\Payments\Reply
     */
    abstract public function callback($ExternalData);

    /**
     * 异步支付回调
     * @param $ExternalData array  支付接口回传的数据
     * @return \Common\Lib\Payments\Reply
     */
    abstract public function notifyCallback($ExternalData);
}